package com.haidechizi.leetcode;

/**
 * @author: wangdejun
 * @create: 2020-04-16 10:16
 **/
public class _861_翻转矩阵后的得分 {

    int result = Integer.MIN_VALUE;

    public int matrixScore(int[][] A) {
        if (A == null || A.length == 0) {
            return 0;
        }
        flipRow(0, A);
        return result;
    }

    public void flipRow(int i, int[][] A) {
        if (i >= A.length) {
            return;
        }
        int[][] temp = copyArr(A);
        A = flipRow(A, i);
        int sum = count(A);
        result = Integer.max(result, sum);
        flipRow(i + 1, A);
        flipRow(i + 1, temp);


    }

    public void flipColumn(int i, int[][] A) {
        int[][] temp = copyArr(A);
        A = flipRow(A, i);
        int sum = count(A);
    }

    private int count(int[][] a) {
        int sum = 0;
        StringBuilder sb = null;
        for (int i = 0; i < a.length; i++) {
            sb = new StringBuilder();
            for (int j = 0; j < a[i].length; j++) {
                sb.append(a[i][j]);
            }
            int value = Integer.parseInt(sb.toString(), 2);
            sum += value;
        }
        return sum;

    }

    private int[][] flipRow(int[][] a, int i) {
        for (int j = 0; j < a[i].length; j++) {
            if (a[i][j] == 0) {
                a[i][j] = 1;
            } else {
                a[i][j] = 0;
            }
        }
        return a;
    }

    private int[][] copyArr(int[][] a) {
        int[][] temp = new int[a.length][a[0].length];
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                temp[i][j] = a[j][j];
            }
        }
        return temp;
    }

    public static void main(String[] args) {
        System.out.println(new _861_翻转矩阵后的得分().matrixScore(new int[][]{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}));
    }

}
